package dao;

import po.StuInfo;
import util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StuDaoImp implements StuDao {

    @Override
    public List<StuInfo> QueryAllStuInfo() {
        List<StuInfo> list = new ArrayList<>();
        try {
            Connection con = DBUtil.getConnection();
            Statement stmt = con.createStatement();
            String sql = "select * from StuInfo";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                StuInfo si = new StuInfo();
                si.setStuId(rs.getString("StuId"));
                si.setStuName(rs.getString("StuName"));
                si.setSex(rs.getBoolean("Sex"));
                si.setAge(rs.getInt("Age"));
                si.setPassword(rs.getString("Password"));
                list.add(si);
            }
            DBUtil.closeConnection(con,null,stmt,rs);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public StuInfo QueryStuInfoById(String stuId) {
      StuInfo stu = new StuInfo();
        try {
            Connection con = DBUtil.getConnection();
            Statement stmt = con.createStatement();
            String sql = "select * from StuInfo where StuId = '" + stuId + "'";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                StuInfo si = new StuInfo();
                si.setStuId(rs.getString("StuId"));
                si.setStuName(rs.getString("StuName"));
                si.setSex(rs.getBoolean("Sex"));
                si.setAge(rs.getInt("Age"));
                si.setPassword(rs.getString("Password"));
                return si;
            }
            DBUtil.closeConnection(con,null,stmt,rs);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int AddStuInfo(StuInfo stuInfo) {
        return 0;
    }

    @Override
    public int UpdateStuInfo(StuInfo stuInfo) {
        return 0;
    }

    @Override
    public int DeleteStuInfoById(String stuId) {
        return 0;
    }
}
